package com.culturesalon.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.culturesalon.connectionfactory.ConnectionFactory;
import com.culturesalon.dto.DeliverInfo;
import com.culturesalon.dto.Orders;
import com.culturesalon.util.OrderState;
import com.culturesalon.util.PaymentWay;

public class DeliverInfoDAOImpl implements DeliverInfoDAO {

	@Override
	public Serializable save(DeliverInfo deliverInfo) {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		try {
			conn=ConnectionFactory.getConnection();
			StringBuffer sql=new StringBuffer();
			sql.append(" INSERT INTO T_DELIVERINFO(ID,ORDERID,USERNAME,PHONE,ADDRESS,POSTCODE) ");
			sql.append(" VALUES(null,?,?,?,?,?) ");
			pstm=conn.prepareStatement(sql.toString(),Statement.RETURN_GENERATED_KEYS);
			pstm.setString(1,deliverInfo.getOrderId());
			pstm.setString(2, deliverInfo.getUserName());
			pstm.setString(3, deliverInfo.getPhone());
			pstm.setString(4, deliverInfo.getAddress());
			pstm.setString(5, deliverInfo.getPostCode());
			pstm.executeUpdate();
			rs=pstm.getGeneratedKeys();
			int id=0;
			if(rs.next()){
				id=rs.getInt(1);
				return id;
			}else{
				
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			ConnectionFactory.close(conn,  rs,pstm);
		}
	}

	@Override
	public Serializable update(DeliverInfo deliverInfo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(Serializable id) {
		// TODO Auto-generated method stub

	}

	@Override
	public DeliverInfo get(Serializable id) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ID,ORDERID,USERNAME,PHONE,ADDRESS,POSTCODE");
		sql.append(" FROM T_DELIVERINFO ");
		sql.append(" WHERE ID=? ");
		conn = ConnectionFactory.getConnection();
		DeliverInfo deliverInfo = null;
		try {
			stmt = conn.prepareStatement(sql.toString());
			stmt.setInt(1, (Integer) id);
			rs = stmt.executeQuery();
			if (rs.next()) {
				deliverInfo=new DeliverInfo();
				deliverInfo.setId(rs.getInt("id"));
				deliverInfo.setOrderId(rs.getString("orderId"));
				deliverInfo.setUserName(rs.getString("userName"));
				deliverInfo.setPhone(rs.getString("phone"));
				deliverInfo.setAddress(rs.getString("address"));
				deliverInfo.setPostCode(rs.getString("postCode"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			ConnectionFactory.close(conn, rs, stmt);
		}
		return deliverInfo;
	}

	@Override
	public List<DeliverInfo> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DeliverInfo getByOrderId(Serializable id) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ID,ORDERID,USERNAME,PHONE,ADDRESS,POSTCODE");
		sql.append(" FROM T_DELIVERINFO ");
		sql.append(" WHERE ORDERID=? ");
		conn = ConnectionFactory.getConnection();
		DeliverInfo deliverInfo = null;
		try {
			stmt = conn.prepareStatement(sql.toString());
			stmt.setString(1, (String) id);
			rs = stmt.executeQuery();
			if (rs.next()) {
				deliverInfo=new DeliverInfo();
				deliverInfo.setId(rs.getInt("id"));
				deliverInfo.setOrderId(rs.getString("orderId"));
				deliverInfo.setUserName(rs.getString("userName"));
				deliverInfo.setPhone(rs.getString("phone"));
				deliverInfo.setAddress(rs.getString("address"));
				deliverInfo.setPostCode(rs.getString("postCode"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			ConnectionFactory.close(conn, rs, stmt);
		}
		return deliverInfo;
	}

}
